/**
 *	 defined the main entry program
 *   VisualGroup JavaScript prototype Classes, version draf
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
/**
 * 	defined VsEngine
 	interface VsEngine {
		function importClass(all fully class name);
		function run();
		// --- defined event ---
		"loaded" => function($this);
	}
 */
VsEngine = new Interface("VsEngine", ["importClass","onReady","run"]);


/**
 * defined engine factory
 */
VsEngineFactory = {
	
	engine:null,
	
	getEngine:function() {
		if (this.engine != null) {
			return this.engine;
		}
		
		// --- build the new engine ---
		this.engine = new FlashVsEngine();
		
		
		return this.engine;
	}
	
}


/**
 * defined flash interaction method
 */
VsEngineCom = {
	
	/**
	 * run the debug mode
	 */
	flash_debug:function($obj) {
		Ext.log("debug outer : " + $obj);
	},
	
	flash_reponseHandle:function ($obj) {
		// --- handle response , return object value ---
		// occur the event method 
		if ($obj == 1) {
	
			var $engine = VsEngineFactory.getEngine();
			try {

				
				$engine.doReady();
				
			} catch (ex) {
				alert(ex);
			}
			
		}


	},
	
	flash_failureHandle:function ($obj) {
		Ext.log("fail handle : " + $obj);
	},
	
	flash_httpStatus:function ($obj) {
		for (var prop in $obj) {
			//Ext.log("$ok : " + prop + " ," +  $obj[prop]);
		}
	}	
	
};

/**
 * extend method
 */
function checkClassExist($className) {
	
	try {
		
		var $obj = eval("new "+$className+"() ");
		return true;
	} catch (e) {
		return false;		
	}
	
}


/**
 * implement engine object 
 */
FlashVsEngine = Ext.extend(Ext.util.Observable, {
	
	constructor:function($config) {

		FlashVsEngine.superclass.constructor.call(this, $config);
		
	},
	
	/**
	 * type of support class handle 
	 * @param {Object} $classes
	 */
	importClass:function($classes) {
		
		var $importType = typeof($classes);
	
		if ($importType == "object" || $importType =="string") {
			
			var $dom = document["flashVsEngineCom"];
			
			try {
				// --- invoke flash import class ---
				if ($classes instanceof Array) {

					for (var $i = 0 ; $i < $classes.length ; $i++) {
						$dom.importClass($classes[$i]);
					}
				}
				else {
					$dom.importClass($classes);
				}
				
			} catch (ex) {
				alert("Occur error in [FlashVsEngine.importClass] : " + ex);
			}
		}
		else {
			throw "the import class format does't supported!";
		}
		
	},
	
	/**
	 * 
	 * @param {Object} fn defined the function object 
	 */
	onReady:function(fn , scope) {

		this.onReadyFn = {
			fn:fn,
			scope: scope
		}
	},
	
	doReady:function() {

		if (this.onReadyFn != null || this.onReadyFn != undefined) {
			this.onReadyFn.fn.call(this.onReadyFn.scope);
		}		
	},
	
	onInit:function(fn,scope) {
		if (scope == null || scope==undefined) {
			scope = this;
		}

		this.onInitFn = {
			fn: fn,
			scope: scope
		};
	},
	
	run:function() {
		var $dom = document["flashVsEngineCom"];
	
		try {
			
			$dom.runEngine();
			
		} catch (ex) {
			alert("Occur error in [FlashVsEngine.run] : " + ex);
		}
		
	},
	
	afterInit:function() {
		
		// --- check the flash engine ready , using 1/1000 sec ---
		var $this = this;
		var times = 0;
		var tryMaxTimes = 100;
		var time = Math.pow(2,times);
		
		var fn = function() {
			// --- over the max times and run the programe fail ---
			try {

				if (times > tryMaxTimes) {
					throw "The try times ovet max times, initial FlashVsEngine fail!Please contact administrator!";
				}
			
				var $dom = document["flashVsEngineCom"];
			
				if ( ($dom.runEngine != null || $dom.runEngine != undefined) 
					&& ($dom.importClass != null || $dom.importClass != undefined) ) {
					if ($this.onInitFn != null || $this.onInitFn != undefined) {
						$this.onInitFn.fn.call($this.onInitFn.scope);
					}
					// --- clear the thread id ---
					clearTimeout(intervalID);	
				}
				else {
					times+=1;
					time = Math.pow(2,times);
					intervalID = setTimeout(fn, time);
				}

				
			} catch (e) {
				Ext.log(e);			
				clearTimeout(intervalID);
			}


		};
		
		var intervalID = setTimeout(fn, time);
		
	}
	
});

/** check interface implement */
(function() {
	try {
		Interface.ensureImplements(new FlashVsEngine(), VsEngine);	
	} catch (e) {
		Ext.log("Class Implement Error :"+"[FlashVsEngine] , " + e);
	}
})();


/** check interface implement */
Ext.onReady(function() {

	var $contextUrl = getRootScriptContent();
	var $movie = $contextUrl+"/depenthird/flash/FlashVsEngine.swf";

	// --- create tag ---
	var $tag = {
		id:"flashVsEngineCom",
		tag:'object',
		classid:"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
		codebase:"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0",
		name:"flashVsEngineCom",
		width:"1",
		height:"1",
		align:"middle",
		children:[
			{tag:"param" , name:"allowScriptAccess" , value:"always" },
			{tag:"param" , name:"allowFullScreen" , value:"false" },
			{tag:"param" , name:"movie" , value:$movie },
			{tag:"param" , name:"quality" , value:"high" },
			{tag:"param" , name:"bgcolor" , value:"#ffffff" },
			{
				tag:"embed" ,
				name:"flashVsEngineCom", 
				src:$movie , 
				bgcolor:"#ffffff",
				allowScriptAccess:"always",
				allowFullScreen:"false",
				type:"application/x-shockwave-flash",
				pluginspage:"http://www.macromedia.com/go/getflashplayer",
				width:"1",
				height:"1",
				align:"middle",
				quality:"high"
			}
		]
	};
	if ( Ext.get("flashVsEngineCom") == null) {
		Ext.DomHelper.append( Ext.getBody() ,  $tag , false);
	}
	
	// --- init method ---
	var $engine = VsEngineFactory.getEngine();

	$engine.afterInit();

});



